package de.hsb.ai.devil.util;

import jade.content.lang.Codec;
import jade.content.lang.sl.SLCodec;
import jade.content.onto.Ontology;
import jade.core.AID;
import jade.core.Agent;
import jade.domain.FIPANames;
import jade.domain.FIPAAgentManagement.ServiceDescription;
import jade.domain.JADEAgentManagement.JADEManagementOntology;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;
import de.hsb.ai.devil.domain.DevilOntology;

/**
 * DevilHelper provides a set of utility-methods. 
 */
public class DevilHelper {

	public static final String MSG_PROTOCOL = FIPANames.InteractionProtocol.FIPA_REQUEST;
	public static final String MSG_LANGUAGE = FIPANames.ContentLanguage.FIPA_SL;
	public static final Ontology MSG_ONTOLOGY = DevilOntology.getInstance();
	public static final Codec MSG_CODEC = new SLCodec();

	private static final DevilHelper instance = new DevilHelper();

	public static DevilHelper getInstance() {
		return instance;
	}
	
	public static ServiceDescription createServiceTemplate() {
		ServiceDescription sd = new ServiceDescription();
		sd.addOntologies(DevilHelper.MSG_ONTOLOGY.getName());
		sd.addLanguages(DevilHelper.MSG_LANGUAGE);
		return sd;
	}
	
	/**
	 * DevilHelper constructor
	 */
	private DevilHelper() {

	}
	
	public static MessageTemplate createMessageTemplate(int...performatives) {
		MessageTemplate mt = createMessageTemplate();
		for (int performative : performatives) 
			mt = MessageTemplate.or(mt, MessageTemplate.MatchPerformative(performative));
		return mt;
	}
	
	public static MessageTemplate createMessageTemplate() {
		return MessageTemplate.and(
				MessageTemplate.MatchProtocol(MSG_PROTOCOL),
				MessageTemplate.and(
						MessageTemplate.MatchOntology(MSG_ONTOLOGY.getName()), 
						MessageTemplate.MatchLanguage(MSG_LANGUAGE)));
	}
	
	public static ACLMessage createAMSRequest(Agent sender) {
		Ontology ontology = JADEManagementOntology.getInstance();
		ACLMessage req = new ACLMessage(ACLMessage.REQUEST);
		req.addReceiver(sender.getAMS());
		req.setProtocol(MSG_PROTOCOL);
		req.setLanguage(FIPANames.ContentLanguage.FIPA_SL0);
		req.setOntology(ontology.getName());

		return req;
	}
	
	public static ACLMessage createDevilRequest(Agent sender, AID receiver, int type) {
		ACLMessage req = new ACLMessage(type);
		req.addReceiver(receiver);
		req.setProtocol(MSG_PROTOCOL);
		req.setLanguage(MSG_LANGUAGE);
		req.setOntology(MSG_ONTOLOGY.getName());
		
		return req;
	}
	
	/**
	 * Teaches specified agent's content-manager how to speak/understand devil.
	 * @param agent
	 */
	public static void learnDevil(Agent agent) {
		agent.getContentManager().registerLanguage(MSG_CODEC, MSG_LANGUAGE);
		agent.getContentManager().registerOntology(MSG_ONTOLOGY);
	}
	
	/**
	 * Teaches specified agent's content-manager how to speak/understand AMS
	 * @param agent
	 */
	public static void learnAMS(Agent agent) {
		agent.getContentManager().registerLanguage(MSG_CODEC, FIPANames.ContentLanguage.FIPA_SL0);
		agent.getContentManager().registerOntology(JADEManagementOntology.getInstance());
	}
	
}
